/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.bootdo.hjh.dao.provider;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class OrderProvider {

    public String queryOrder(Map<String,Object> map){
        Integer offset =(Integer) map.get("offset");
        Integer limit =(Integer) map.get("limit");
        Integer status =(Integer) map.get("status");
        String mobile =(String) map.get("mobile");
        String receiver =(String) map.get("receiver");
        String orderNo = (String) map.get("orderNo");
        StringBuilder sb = new StringBuilder();

        sb.append("select * from t_order where 1>0 ");

        if(status!=null){
            sb.append(" and order_status = #{status}");
        }
        if(StringUtils.isNotBlank(orderNo)){
            sb.append(" and order_no like concat('%',#{orderNo},'%') ");
        }
        if(StringUtils.isNotEmpty(mobile)) {
        	sb.append(" and mobile like concat('%',#{mobile},'%') ");
        }
        if(StringUtils.isNotEmpty(receiver)) {
        	sb.append(" and receiver like concat('%',#{receiver},'%') ");
        }
        sb.append(" order by id desc");
        if(offset!=null && limit!=null){
            sb.append(" limit #{offset},#{limit}");
        }
        return sb.toString();
    }

    public String countOrder(Map<String,Object> map){
        Integer status =(Integer) map.get("status");
        String orderNo = (String) map.get("orderNo");
        String mobile =(String) map.get("mobile");
        String receiver =(String) map.get("receiver");
        StringBuilder sb = new StringBuilder();

        sb.append("select count(1) from t_order where 1>0 ");

        if(status!=null){
            sb.append(" and order_status = #{status}");
        }

        if(StringUtils.isNotBlank(orderNo)){
            sb.append(" and order_no like concat('%',#{orderNo},'%') ");
        }
        if(StringUtils.isNotEmpty(mobile)) {
        	sb.append(" and mobile like concat('%',#{mobile},'%') ");
        }
        if(StringUtils.isNotEmpty(receiver)) {
        	sb.append(" and receiver like concat('%',#{receiver},'%') ");
        }
        return sb.toString();

    }


    public String queryWithdraw(Map<String,Object> map){
        Integer offset =(Integer) map.get("offset");
        Integer limit =(Integer) map.get("limit");
        Integer status =(Integer) map.get("status");
        Long userId =(Long) map.get("userId");
        StringBuilder sb = new StringBuilder();

        sb.append("select * from withdraw where 1>0 ");

        if(status!=null){
            sb.append(" and status = #{status}");
        }

        if(userId!=null){
            sb.append(" and user_id = #{userId}");
        }
        sb.append(" order by id desc");
        if(offset!=null && limit!=null){
            sb.append(" limit #{offset},#{limit}");
        }
        return sb.toString();
    }

    public String countWithdraw(Map<String,Object> map){
        Integer status =(Integer) map.get("status");
        Long userId =(Long) map.get("userId");
        StringBuilder sb = new StringBuilder();

        sb.append("select count(1) from withdraw where 1>0 ");

        if(status!=null){
            sb.append(" and status = #{status}");
        }

        if(userId!=null){
            sb.append(" and user_id = #{userId}");
        }
        return sb.toString();
    }

    public String updateWithdrawStatus(Map<String,Object> map){
        StringBuilder sb = new StringBuilder();
        Integer status = (Integer) map.get("status");
        sb.append("update withdraw set status=#{status},last_modify_time=now()");
        if(status == 1){
            sb.append(",remit_time=now()");
        }
        sb.append(" where id=#{id}");
        return sb.toString();
    }
}
